package com.gitee.wsl.compose.chart.xy.model

import com.gitee.wsl.compose.chart.ChartAdapter

typealias ListPointEntry<X,Y> = PointEntry<X,List<Y>>

fun <X,Y> List<PointEntry<X,Y>>.toListPointEntry():List<ListPointEntry<X,Y>> = map {
        ListPointEntry(it.x,listOf(it.y))
    }


//interface ListPointEntry<X,Y>: ChartEntry {
//    /**
//     * The x-axis value of this Point.
//     */
//    val x: X
//
//    /**
//     * The y-axis value of this Point.
//     */
//    val y: List<Y>
//}

fun<X,Y> ListPointEntry(x:X, y:List<Y>):ListPointEntry<X,Y>{
    return object:ListPointEntry<X,Y>{
        override val x: X = x
        override val y: List<Y> = y
    }
}


fun <X, Y> ChartAdapter.Companion.mutableValueLines(builder: MutableValueLinesBuilder<X, Y>.()->Unit):List<List<ListPointEntry<X,Y>>>{
    val lines = mutableListOf<List<ListPointEntry<X,Y>>>()
    object:MutableValueLinesBuilder<X, Y>{
        override fun line(builder: MutableValueLineBuilder<X, Y>.()->Unit) {
            val line = mutableListOf<ListPointEntry<X,Y>>()
            object:MutableValueLineBuilder<X, Y>{
                override fun at(x: X, y: Y,vararg ys:Y): Pair<X, Y> {
                   val listY = mutableListOf<Y>()
                    listY.add(y)
                    listY.addAll(ys)
                    line.add(ListPointEntry(x,listY))
                    return x to y
                }

                override fun Pair<X,Y>.lineTo(x:X, y:Y,vararg ys:Y): Pair<X, Y> {
                    val listY = mutableListOf<Y>()
                    listY.add(y)
                    listY.addAll(ys)
                    line.add(ListPointEntry(x,listY))
                    return x to y
                }
            }
            lines.add(line)
        }
    }.builder()
    return lines
}


interface MutableValueLinesBuilder<X, Y> {
    fun line(builder: MutableValueLineBuilder<X, Y>.()->Unit)
}

interface MutableValueLineBuilder<X, Y> {

    fun at(x:X, y:Y,vararg ys:Y):Pair<X,Y>

    fun Pair<X,Y>.lineTo(x:X, y:Y, vararg ys:Y):Pair<X,Y>
}